<template>
	<view class="content">
		<u-sticky>
			<!-- 只能有一个根元素 -->
			<view class="sticky">
				提示：本次评价内容总部质控可以查看，为了改善服务质量和配送体验，请如实放心填写。
			</view>
		</u-sticky>
		<view class="formclass">
			<u-form :model="form" ref="uForm" label-width="140rpx">
				<u-line class="u-line"></u-line>
				<u-section title="仓库" :right="false"></u-section>
				<u-form-item label="仓库装车" prop="whLoadScore">
					<u-rate :count="rateCnt" v-model="form.whLoadScore"></u-rate>
				</u-form-item>
				<u-form-item label="仓库服务" prop="whServScore">
					<u-rate :count="rateCnt" v-model="form.whServScore"></u-rate>
				</u-form-item>
				<u-form-item label="投诉建议" prop="remark">
					<u-input v-model="form.remark" type="textarea" border="true" maxlength="200"  />
				</u-form-item>
				<u-line class="u-line"></u-line>
				<u-section title="结算" :right="false"></u-section>
				<u-form-item label="拖欠运费" prop="payFlag">
					<u-radio-group v-model="form.payFlag">
						<u-radio 
							v-for="(item, index) in tqyfList" :key="index" 
							:name="item.name"
						>
							{{item.name}}
						</u-radio>
					</u-radio-group>
				</u-form-item>
				<u-form-item label="投诉建议" prop="payRemark">
					<u-input v-model="form.payRemark" type="textarea" placeholder="本投诉针对超出结算时间或者承运商恶意拖欠的，请描述清楚拖欠人或者拖欠公司"  border="true" maxlength="200"  />
				</u-form-item>
				<view v-for="(item,index) in form.storeList" :key="index">
					<u-line class="u-line"></u-line>
					<u-section :title="item.storeName" :right="false"></u-section>
					<u-form-item label="卸车速度">
						<u-rate :count="rateCnt" v-model="item.unloadingSpeed"></u-rate>
					</u-form-item>
					<u-form-item label="门店态度">
						<u-rate :count="rateCnt" v-model="item.storeAttitude"></u-rate>
					</u-form-item>
					<u-form-item label="投诉建议">
						<u-input v-model="item.storeRemark" type="textarea" border="true" maxlength="200"  />
					</u-form-item>
				</view>
			</u-form>
			<u-button type="primary" @click="submit" v-if="btshow">提交</u-button>
		</view>
		<view>
			<u-toast ref="uToast" />
		</view>
	</view>
</template>

<script>
	// let plugin = requirePlugin("WechatSI");
	// let manager = plugin.getRecordRecognitionManager();
	
	import config from '../../../config.js'
	export default {
		data() {
			return {
				tqyfList:[{name:'是'},{name:'否'}],
				rateCnt: 5,
				btshow: true,
				form:{
					id: null,
					dispatchNumber: null,
					whLoadScore: 3,
					whServScore: 3,
					storeList:[],
					remark: null,
				},
				rules: {
					remark: [
						{
							min: 5,
							message: '评价不能少于5个字',
							trigger: 'change'
						}
					]
				}
				
			}
		},
		onLoad(open) {
			this.form.dispatchNumber = open.dispatchNumber;
			this.$http.post('/rest/tDispatchDriverEval/getInfoByDispatchNumber',{
			},{
				params: {
					dispatchNumber:open.dispatchNumber
				},
				header: {
					'Authorization' :uni.getStorageSync('token'),
				}
			}).then(res => {
				if(res.data.code == 200 && res.data.data != ""){
					this.form.id = res.data.data.id;
					this.form.whLoadScore = res.data.data.whLoadScore;
					this.form.whServScore = res.data.data.whServScore;
					this.form.payFlag = res.data.data.payFlag;
					this.form.payRemark = res.data.data.payRemark;
					this.form.remark = res.data.data.remark;
					this.form.storeList = res.data.data.storeList;
					console.log(this.form);
					if(this.form.id != ""){
						this.btshow = false;
					}
					// plugin.textToSpeech({
					// 	lang: "zh_CN",
					// 	tts: true,
					// 	content: '恭喜您，已完成送达，等待本单完成后，可以针对门店进行评价建议',
					// 	success: function(res) {
					// 		let music = null;
					// 		music = uni.createInnerAudioContext(); //创建播放器对象
					// 		music.src = res.filename;
					// 		music.play(); //执行播放
					// 		music.onEnded(() => {
					// 			//播放结束
					// 			music = null;
					// 		});
					// 	},
					// 	fail: function(res) {
					// 		 // console.log("fail tts", res)
					// 	}
					// })
					
				}
			})
		},
		// 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			submit() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						console.log(this.form);
						uni.showLoading({
							title: '加载中...'
						})
						uni.request({
							method:"POST",
							header:{
								'Authorization' :uni.getStorageSync('token'),
							},
							url: config.root +'rest/tDispatchDriverEval/add',
							data:this.form,
							success: (res) => {
								uni.hideLoading()
								if(res.data.success){
									if(res.data.code == 200){
										this.$refs.uToast.show({
											title: '操作成功',
											type: 'success'
										})
										this.btshow = false;
									}else{
										this.$refs.uToast.show({
											title: res.data.message,
											type: 'error'
										})
									}
								} else {
									uni.showToast({
										title: res.data.message,
										duration: 2000,
										icon:'none',
									});
								}
							}
						})
						
						// let formData = encodeURI(JSON.stringify(this.form));
						// this.$http.post('/rest/tDispatchDriverEval/add',{
						// },{
						// 	params: {"formData":formData},
						// 	header: {
						// 		'Authorization' :uni.getStorageSync('token'),
						// 	}
						// }).then(res => {
						// 	if(res.data.code == 200){
						// 		this.$refs.uToast.show({
						// 			title: '操作成功',
						// 			type: 'success'
						// 		})
						// 		this.btshow = false;
						// 	}else{
						// 		this.$refs.uToast.show({
						// 			title: res.data.message,
						// 			type: 'error'
						// 		})
						// 	}
						// })
					} else {
						console.log('验证失败');
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	.content {
		margin-top: 12rpx;
	}
	
	.sticky {
		width: 750rpx;
		height: 120rpx;
		background-color: #2979ff;
		color: #fff;
		padding: 24rpx;
	}
	.formclass {
		margin-top: 20rpx;
	}
</style>